<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Admin_Sexualorientations extends Admin_Controller {
	public function __construct()
	{
		parent::__construct();
		$this->lang->load('sexualorientation');
	}
	public function ajax_sexual_orientation_get(){
		$sexual_orientationEntity =  new CI_Sexual_orientation_entity();
		$sexual_orientation = $sexual_orientationEntity->getSexualOrientationByCode($_REQUEST['code']);
		echo json_encode($sexual_orientation);
	}
	public function ajax_sexual_orientation_list(){
		$sexual_orientationEntity =  new CI_Sexual_orientation_entity();
		$escortSettings = new CI_Escort_setting_entity();
		$defaultLanguageId = $escortSettings->getDefaultLanguageId();
		$sexualOrientationList = $sexual_orientationEntity->getSexualOrientationListByLanguage($defaultLanguageId);
		$data['sexualOrientationList'] = $sexualOrientationList;
		
		$languageEntity = new CI_Language_entity();
		$languageList = $languageEntity->getLanguageList();
		$data['languageList'] = $languageList;
		
		$this->load->view('admin/sexualorientations/index', $data);
	}
	public function ajax_sexual_orientation_save(){
		$sexual_orientationNameList = $_REQUEST['sexual_orientation'];
		$sexual_orientationNameDefault = '';
		foreach ($sexual_orientationNameList as $key => $value) {
			if($value != null && $value != ""){
				$sexual_orientationNameDefault = $value;
				break;
			}
		}
		$result = array();
		if($sexual_orientationNameDefault == ''){
			$result['result'] = false;
			$result['message'] = lang('escort_sexualorientation_require');
		}else{
			if($this->_check_sexual_orientation(0, $sexual_orientationNameDefault)){
				$result['result'] = false;
				$result['message'] = lang('escort_sexualorientation_existed');
			}else{
				$code = 0;
				foreach ($sexual_orientationNameList as $key => $value) {
					$sexual_orientationEntity =  new CI_Sexual_orientation_entity();
					if($value != null && $value != ''){
						$sexual_orientationEntity->setOrientation($value);
					}else{
						$sexual_orientationEntity->setOrientation($sexual_orientationNameDefault);
					}
					$sexual_orientationEntity->setLanguageId($key);
					$sexual_orientationEntity->setCode($code);
					$id = $sexual_orientationEntity->save();
					if($code == 0){
						$code = $id;
						$sexual_orientationEntity->setId($id);
						$sexual_orientationEntity->setCode($code);
						$sexual_orientationEntity->update();
					}
				}
				$result['result'] = true;
				$result['message'] =lang('escort_sexualorientation_save_success');
				$result['id'] =$id;
			}
		}
		echo json_encode($result);
	}
	public function ajax_sexual_orientation_update(){
		$sexual_orientationNameList = $_REQUEST['sexual_orientation'];
		$sexual_orientationIdList = $_REQUEST['sexual_orientation_id'];
		$sexual_orientationCode = $_REQUEST['sexualorientation_code'];
		$sexual_orientationNameDefault = '';
		foreach ($sexual_orientationNameList as $key => $value) {
			if($value != null && $value != ""){
				$sexual_orientationNameDefault = $value;
				break;
			}
		}
		$result = array();
		if($sexual_orientationNameDefault == ''){
			$result['result'] = false;
			$result['message'] = lang('escort_sexualorientation_require');
		}else{
			$keys = array_keys($sexual_orientationIdList);
			if($this->_check_sexual_orientation($sexual_orientationIdList[$keys[0]], $sexual_orientationNameDefault)){
				
				$result['result'] = false;
				$result['message'] = lang('escort_sexualorientation_existed');
			}else{
				foreach ($sexual_orientationNameList as $key => $value) {
					$sexual_orientationEntity =  new CI_Sexual_orientation_entity();
					if($value != null && $value != ''){
						$sexual_orientationEntity->setOrientation($value);
					}else{
						$sexual_orientationEntity->setOrientation($sexual_orientationNameDefault);
					}
					$sexual_orientationEntity->setLanguageId($key);
					$sexual_orientationEntity->setCode($sexual_orientationCode);
					$sexual_orientationEntity->setId($sexual_orientationIdList[$key]);
					$sexual_orientationEntity->update();
				}
				$result['result'] = true;
				$result['message'] =lang('escort_sexualorientation_save_success');
			}
		}
		
		echo json_encode($result);
	}
	public function ajax_sexual_orientation_delete(){
		$result = array();
		$sexual_orientationEntity =  new CI_Sexual_orientation_entity();
		$result['result'] = $sexual_orientationEntity->deleteByCode($_REQUEST['code']);
		if($result['result']){
			$result['message'] = lang('escort_sexualorientation_delete_success');
		}else{
			$result['message'] = lang('escort_sexualorientation_delete_error');
		}
		echo json_encode($result);
	}
	function _check_sexual_orientation($id, $name){
		$ci = &get_instance();
		return $ci->sexual_orientation_entity->checkExistedName($id, $name);
	}
}
?>